package com.sptus.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sptus.common.core.domain.AjaxResult;
import com.sptus.system.domain.DevDictionaries;
import com.sptus.system.domain.DevInfoMsg1History;
import com.sptus.system.domain.DeviceModel;
import com.sptus.system.domain.Equipment;
import com.sptus.system.domain.model.DevModel;
import com.sptus.system.domain.vo.AppInfo;
import com.sptus.web.mapper.master.CloudEquipmentMapper;
import com.sptus.web.service.CloudIEquipmentService;
import com.sptus.web.util.DevSqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CloudEquipmentServiceImpl extends ServiceImpl<CloudEquipmentMapper, Equipment> implements CloudIEquipmentService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public AjaxResult getEquipmentInfo(AppInfo appInfo, Equipment equipment) {
        ArrayList<DevDictionaries> devDictionaries = new ArrayList<>();
        DevDictionaries devDictionaries1 = new DevDictionaries();
        devDictionaries1.setCode("ICT");
        devDictionaries1.setName("市电总有功功率");
        devDictionaries1.setValue("");
        devDictionaries1.setUnit("W");
        devDictionaries1.setRelevanceTable("dev_info_msg0");
        //工作模式
        DevDictionaries devDictionaries2 = new DevDictionaries();
        devDictionaries2.setCode("OPT");
        devDictionaries2.setName("负载总有功功率");
        devDictionaries2.setValue("");
        devDictionaries2.setUnit("W");
        devDictionaries2.setRelevanceTable("dev_info_msg0");
        //AC输出频率
        DevDictionaries devDictionaries3 = new DevDictionaries();
        devDictionaries3.setCode("GFO");
        devDictionaries3.setName("AC输出频率");
        devDictionaries3.setValue(" 55.57");
        devDictionaries3.setUnit("Hz");
        devDictionaries3.setOperation("x/100");
        devDictionaries3.setAnalysis("{\"valid\":\"2\"}");
        devDictionaries3.setRelevanceTable("dev_info_msg0");

        DevDictionaries devDictionaries4 = new DevDictionaries();
        devDictionaries4.setCode("SV1");
        devDictionaries4.setName(" PV1电压");
        devDictionaries4.setOperation("x/10");
        devDictionaries4.setUnit("V");
        devDictionaries4.setRelevanceTable("dev_info_msg0");

        DevDictionaries devDictionaries5 = new DevDictionaries();
        devDictionaries5.setCode("SV2");
        devDictionaries5.setName(" PV2电压");
        devDictionaries5.setOperation("x/10");
        devDictionaries5.setUnit("V");
        devDictionaries5.setRelevanceTable("dev_info_msg0");

        DevDictionaries devDictionaries6 = new DevDictionaries();
        devDictionaries6.setCode("SC1");
        devDictionaries6.setName(" PV1电流");

        devDictionaries6.setUnit("A");
        devDictionaries6.setOperation("x/10");
        devDictionaries6.setRelevanceTable("dev_info_msg0");

        DevDictionaries devDictionaries7 = new DevDictionaries();
        devDictionaries7.setCode("SC2");
        devDictionaries7.setName(" PV2电流");

        devDictionaries7.setUnit("A");
        devDictionaries7.setOperation("x/10");
        devDictionaries7.setRelevanceTable("dev_info_msg0");
        DevDictionaries devDictionaries8 = new DevDictionaries();
        devDictionaries8.setCode("SP1");
        devDictionaries8.setName(" PV1功率");
        devDictionaries8.setValue("78.9");
        devDictionaries8.setUnit("W");
        devDictionaries8.setRelevanceTable("dev_info_msg0");

        DevDictionaries devDictionaries9 = new DevDictionaries();
        devDictionaries9.setCode("SP2");
        devDictionaries9.setName(" PV2功率");

        devDictionaries9.setValue("78.9");
        devDictionaries9.setUnit("W");
        devDictionaries9.setRelevanceTable("dev_info_msg0");

        DevDictionaries devDictionaries10 = new DevDictionaries();
        devDictionaries10.setCode("TI");
        devDictionaries10.setName("逆变器温度");
        devDictionaries10.setValue("35");
        devDictionaries10.setUnit("℃");
        devDictionaries10.setRelevanceTable("dev_info_msg0");

        devDictionaries.add(devDictionaries1);
        devDictionaries.add(devDictionaries2);
        devDictionaries.add(devDictionaries3);
        devDictionaries.add(devDictionaries4);
        devDictionaries.add(devDictionaries5);
        devDictionaries.add(devDictionaries6);
        devDictionaries.add(devDictionaries7);
        devDictionaries.add(devDictionaries8);
        devDictionaries.add(devDictionaries9);
        devDictionaries.add(devDictionaries10);

         DevSqlUtil.DevDataSql(devDictionaries, jdbcTemplate, "96142309600264");
        equipment.setDevDictionaries(devDictionaries);
        return  AjaxResult.success("查询成功", equipment);
    }


    @Override
    public AjaxResult getInverterInfo(AppInfo appInfo, Equipment equipment) {
        ArrayList<DevDictionaries> devDictionaries = new ArrayList<>();
        DevDictionaries devDictionaries1 = new DevDictionaries();
        devDictionaries1.setCode("GFI");
        devDictionaries1.setName("市电总有功功率");
        devDictionaries1.setValue("100");
        devDictionaries1.setUnit("Hz");
        devDictionaries1.setRelevanceTable("");

        DevDictionaries devDictionaries2 = new DevDictionaries();
        devDictionaries2.setCode("BV");
        devDictionaries2.setName("电池电压");
        devDictionaries2.setValue("528");
        devDictionaries2.setUnit("V");
        //AC输出频率
        DevDictionaries devDictionaries3 = new DevDictionaries();
        devDictionaries3.setCode("GFO");
        devDictionaries3.setName("AC输出频率");
        devDictionaries3.setValue(" 55.57");
        devDictionaries3.setUnit("Hz");

        DevDictionaries devDictionaries4 = new DevDictionaries();
        devDictionaries4.setCode("SV1");
        devDictionaries4.setName(" PV1电压");
        devDictionaries4.setValue("78.9");
        devDictionaries4.setUnit("V");

        DevDictionaries devDictionaries5 = new DevDictionaries();
        devDictionaries5.setCode("SV2");
        devDictionaries5.setName(" PV2电压");
        devDictionaries5.setValue("78.9");
        devDictionaries5.setUnit("V");

        DevDictionaries devDictionaries6 = new DevDictionaries();
        devDictionaries6.setCode("SC1");
        devDictionaries6.setName(" PV1电流");
        devDictionaries6.setValue("78.9");
        devDictionaries6.setUnit("A");

        DevDictionaries devDictionaries7 = new DevDictionaries();
        devDictionaries7.setCode("SC2");
        devDictionaries7.setName(" PV2电流");
        devDictionaries7.setValue("78.9");
        devDictionaries7.setUnit("A");
        DevDictionaries devDictionaries8 = new DevDictionaries();
        devDictionaries8.setCode("SP1");
        devDictionaries8.setName(" PV1功率");
        devDictionaries8.setValue("78.9");
        devDictionaries8.setUnit("W");
        DevDictionaries devDictionaries9 = new DevDictionaries();
        devDictionaries9.setCode("SP1");
        devDictionaries9.setName(" PV2功率");
        devDictionaries9.setValue("78.9");
        devDictionaries9.setUnit("W");


        devDictionaries.add(devDictionaries1);
        devDictionaries.add(devDictionaries2);
        devDictionaries.add(devDictionaries3);
        devDictionaries.add(devDictionaries4);
        devDictionaries.add(devDictionaries5);
        devDictionaries.add(devDictionaries6);
        devDictionaries.add(devDictionaries7);
        devDictionaries.add(devDictionaries8);
        devDictionaries.add(devDictionaries9);


        equipment.setDevDictionaries(devDictionaries);
        return AjaxResult.success(equipment);
    }

    @Override
    public AjaxResult getEquipmentInfoData(AppInfo appInfo, Equipment equipment) {
        ArrayList<DevDictionaries> devDictionaries = new ArrayList<>();
        DevDictionaries devDictionaries1 = new DevDictionaries();
        devDictionaries1.setCode("WFV");
        devDictionaries1.setName("WiFi版本");
        devDictionaries1.setValue("FC41DAAR11A04_RYY.bin_202406041002");
        devDictionaries1.setUnit("");
        devDictionaries1.setRelevanceTable("dev_info_msg3");

        //工作模式
        DevDictionaries devDictionaries2 = new DevDictionaries();
        devDictionaries2.setCode("`FOR`");
        devDictionaries2.setName("额定频率");
        devDictionaries2.setValue("50");
        devDictionaries2.setUnit("Hz");
        devDictionaries2.setRelevanceTable("dev_info_msg3");
        //AC输出频率
        DevDictionaries devDictionaries3 = new DevDictionaries();
        devDictionaries3.setCode("IV");
        devDictionaries3.setName("内部协议版本");
        devDictionaries3.setValue(" 1.0.1");
        devDictionaries3.setUnit("");
        devDictionaries3.setRelevanceTable("dev_info_msg3");

        DevDictionaries devDictionaries4 = new DevDictionaries();
        devDictionaries4.setCode("VOR");
        devDictionaries4.setName(" 额定输出电压");
        devDictionaries4.setValue("1200");
        devDictionaries4.setUnit("V");
        devDictionaries4.setOperation("x/10");
        devDictionaries4.setRelevanceTable("dev_info_msg3");

        DevDictionaries devDictionaries5 = new DevDictionaries();
        devDictionaries5.setCode("VIR");
        devDictionaries5.setName(" 额定输入电压");
        devDictionaries5.setValue("1200");
        devDictionaries5.setUnit("V");
        devDictionaries5.setOperation("x/10");
        devDictionaries5.setRelevanceTable("dev_info_msg3");

        DevDictionaries devDictionaries6 = new DevDictionaries();
        devDictionaries6.setCode("CIR");
        devDictionaries6.setName("额定输入电流");
        devDictionaries6.setValue("500");
        devDictionaries6.setUnit("A");
        devDictionaries6.setOperation("x/10");
        devDictionaries6.setRelevanceTable("dev_info_msg3");

        DevDictionaries devDictionaries7 = new DevDictionaries();
        devDictionaries7.setCode("COR");
        devDictionaries7.setName(" 额定输出电流");
        devDictionaries7.setValue("500");
        devDictionaries7.setOperation("x/10");
        devDictionaries7.setUnit("A");
        devDictionaries7.setRelevanceTable("dev_info_msg3");
//        DevDictionaries devDictionaries8 = new DevDictionaries();
//        devDictionaries8.setCode("SP1");
//        devDictionaries8.setName(" PV1功率");
//        devDictionaries8.setValue("78.9");
//        devDictionaries8.setUnit("W");
//        DevDictionaries devDictionaries9 = new DevDictionaries();
//        devDictionaries9.setCode("SP1");
//        devDictionaries9.setName(" PV2功率");
//        devDictionaries9.setValue("78.9");
//        devDictionaries9.setUnit("W");


        devDictionaries.add(devDictionaries1);
        devDictionaries.add(devDictionaries2);
        devDictionaries.add(devDictionaries3);
        devDictionaries.add(devDictionaries4);
        devDictionaries.add(devDictionaries5);
        devDictionaries.add(devDictionaries6);
        devDictionaries.add(devDictionaries7);
        ArrayList<DevDictionaries> devDictionaries8 = (ArrayList<DevDictionaries>) DevSqlUtil.DevDataSql(devDictionaries, jdbcTemplate, "96142309600264");
        devDictionaries2.setCode("FOR");
//        devDictionaries.add(devDictionaries8);
//        devDictionaries.add(devDictionaries9);


        equipment.setDevDictionaries(devDictionaries8);
        return AjaxResult.success(equipment);
    }

    @Override
    public HashMap<String, String> getEquipmentCity(String sn) {

        return this.getBaseMapper().getEquipmentCity(sn);
    }

    @Override
    public ArrayList<HashMap<String, String>> getDevClassify(DevModel devModel) {

        return this.getBaseMapper().getDevClassify(devModel);
    }

    @Override
    public ArrayList<HashMap<String, String>> getHomeHead(DevModel devModel) {
        return this.getBaseMapper().getHomeHead(devModel);
    }

    @Override
    public ArrayList<HashMap<String, String>> getDevGeneration(DevModel devModel) {
        if(devModel.getDateType().equals("day")){
            return this.getBaseMapper().getDevDayGeneration(devModel);
        }else   if(devModel.getDateType().equals("month")){
            return this.getBaseMapper().getDevMonthGeneration(devModel);
        }else  if(devModel.getDateType().equals("year")){
            return this.getBaseMapper().getDevYearGeneration(devModel);
        }
        return null;
    }

    @Override
    public HashMap<String, Object> getDevHealth(DevModel devModel) {
        return this.getBaseMapper().getDevHealth(devModel);
    }

    @Override
    public ArrayList<HashMap<String, Object>> getDevHealthFloat(DevModel devModel) {
        return this.getBaseMapper().getDevHealthFloat(devModel);
    }

    @Override
    public ArrayList<HashMap<String, Object>> getDevAlarmList(DevModel devModel) {

        return  this.getBaseMapper().getDevAlarmList(devModel);
    }

    @Override
    public void saveHomeHealth(HashMap<String, Object> devBG) {
          this.getBaseMapper().saveHomeHealth(devBG);
    }

    @Override
    public HashMap<String, Object> selectListPage(DeviceModel deviceModel) {
        deviceModel.setPageNo((deviceModel.getPageNo()-1)*deviceModel.getPageSize());
        int i = this.getBaseMapper().selectListCount(deviceModel);
        ArrayList<HashMap<String, Object>> arrayList = this.getBaseMapper().selectListPage(deviceModel);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("list",arrayList);
        stringObjectHashMap.put("count",i);
        return stringObjectHashMap;
    }

    @Override
    public ArrayList<HashMap<String, String>> getDevMap(List<Integer> number) {
        String numbers = number.stream().map(String::valueOf).collect(Collectors.joining(","));
        return this.getBaseMapper().getDevMap(numbers);
    }

    public ArrayList<HashMap<String, Object>> getEquipmentGenration() {
        return  this.getBaseMapper().getEquipmentGenration();
    }

    public void saveGapacity(HashMap<String, Object> devBG) {
        this.getBaseMapper().saveGapacity(devBG);
    }

    public HashMap<String, Object> getHomeHealth(String number) {
       return this.getBaseMapper().getHomeHealth(number);

    }

    public ArrayList<HashMap<String, Object>> getDevTypeNum() {
        return this.getBaseMapper().getDevTypeNum();
    }

    @Override
    public List<Equipment> selectListInfo(QueryWrapper<Equipment> equipmentLambdaQueryWrapper) {
        return this.getBaseMapper().selectListInfo(equipmentLambdaQueryWrapper);
    }

    public HashMap<String, Object> selectHistoryDataSetInfoPage(AppInfo appInfo, QueryWrapper<DevInfoMsg1History> queryWrapper) {
        return null;
    }

    public IPage<HashMap<String, Object>> getHistoryDataSetInfoPage(Page<DevInfoMsg1History> page, QueryWrapper<DevInfoMsg1History> queryWrapper) {

        return this.getBaseMapper().getHistoryDataSetInfoPage(page,queryWrapper);
    }

    public HashMap<String, Object> getDeviceUpgrade(DeviceModel deviceModel) {
        return this.getBaseMapper().getDeviceUpgrade(deviceModel);
    }

    public HashMap<String, Object> getDeviceState(DeviceModel deviceModel) {
        return this.getBaseMapper().getDeviceState(deviceModel);
    }
}
